//
//  ViewController.m
//  22-QQ音乐MV界面
//
//  Created by dllo on 16/8/12.
//  Copyright © 2016年 DM.C. All rights reserved.
//

#import "ViewController.h"
#import "AFNetworking.h"
#import "CustomCollectionViewCell.h"
#import "TitleHeadCollectionReusableView.h"
#import "AllData.h"
#import "Data.h"
#import "Contents.h"
#import "Mvlist.h"
#import "List.h"
#import "CycleCollectionViewCell.h"
#import "CycleCollectionReusableView.h"
@interface ViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (weak, nonatomic) IBOutlet UICollectionView *collectionVIew;
@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *layout;
@property (nonatomic, strong) AllData *allData;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [_collectionVIew registerClass:[CycleCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"cycleReuse"];
    [self createNaviBarItem];
    [self mv_show_playData];
    [self layoutItems];
}
- (void)layoutItems {
    _layout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
    _layout.minimumLineSpacing = 5;
    _layout.minimumInteritemSpacing = 5;
}
- (void)createNaviBarItem {
    UILabel *yinyueguan = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 70, 44)];
    yinyueguan.center = CGPointMake(self.navigationController.navigationBar.center.x, 20);
    yinyueguan.text = @"音乐馆";
    //    yinyueguan.backgroundColor = [UIColor cyanColor];
    yinyueguan.font = [UIFont systemFontOfSize:20];
    yinyueguan.textColor = [UIColor whiteColor];
    yinyueguan.textAlignment = NSTextAlignmentCenter;
    [self.navigationController.navigationBar addSubview:yinyueguan];
    UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 60, 44)];
    myLabel.center = CGPointMake(self.navigationController.navigationBar.center.x - 70, 20);
    myLabel.text = @"我的";
    //    myLabel.backgroundColor = [UIColor cyanColor];
    myLabel.font = [UIFont systemFontOfSize:20];
    myLabel.textColor = [UIColor whiteColor];
    myLabel.textAlignment = NSTextAlignmentCenter;
    [self.navigationController.navigationBar addSubview:myLabel];
    
    UILabel *foundLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 60, 44)];
    foundLabel.center = CGPointMake(self.navigationController.navigationBar.center.x + 70, 20);
    foundLabel.text = @"发现";
    //    foundLabel.backgroundColor = [UIColor cyanColor];
    foundLabel.font = [UIFont systemFontOfSize:20];
    foundLabel.textColor = [UIColor whiteColor];
    foundLabel.textAlignment = NSTextAlignmentCenter;
    [self.navigationController.navigationBar addSubview:foundLabel];
    

}
- (void)mv_show_playData {
    
    AFHTTPSessionManager *manger = [AFHTTPSessionManager manager];
    NSString *requestURLString = @"http://i.y.qq.com/v8/fcg-bin/getmv_by_tag?cmd=mv_shows_new&g_tk=5381&uin=0&format=json&inCharset=utf-8&outCharset=utf-8&notice=0&platform=h5&needNewCode=1&_=1470996835294";
    // 1. requestSerializer 格式化Body/参数
    manger.requestSerializer = [AFHTTPRequestSerializer serializer];

    // 2.responseSerializer 返回数据, 进行格式化结果
    manger.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    // 保留系统中的ContentType, 添加新的Type
    NSMutableSet *set = [NSMutableSet setWithSet:manger.responseSerializer.acceptableContentTypes];
    [set addObject:@"text/html"];
    [set addObject:@"application/x-javascript"];
    
    // responseSerializer默认类型 AFJSONResponseSerializer
    [manger.responseSerializer setAcceptableContentTypes:set];
    
    [manger GET:requestURLString parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"*****%@", task.currentRequest.URL);
        NSLog(@"请求成功%@", responseObject);
        NSError *error = nil;
         NSDictionary *content = [NSJSONSerialization JSONObjectWithData:responseObject
                                                                 options:NSJSONReadingMutableContainers
                                                                   error:&error];//转换数据格式
        NSLog(@"sdaf sf%@", content);
        _allData = [AllData modelWithDic:content];
        [self.collectionVIew reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"*****%@", task.currentRequest.URL);
        NSLog(@"请求失败%@", error);
        
    }];

}
- (void)mvContentData {
    
}
#pragma mark - dataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return _allData.data.contents.list.count;
}

- (NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _allData.data.contents.list[section].mvlist.count;
    
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
                           cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    CustomCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    cell.mvList = _allData.data.contents.list[indexPath.section].mvlist[indexPath.row];
    return cell;
    
}
#pragma mark - 返回头视图的reuseable
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        CycleCollectionReusableView *reuseable = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"cycleReuse" forIndexPath:indexPath];
        reuseable.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), 150);
        reuseable.mv_show_play = _allData.data.mv_show_play;
        return reuseable;
    } else {
    
    
     TitleHeadCollectionReusableView *reuseableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headReuse" forIndexPath:indexPath];
        reuseableView.titleLabel.text = _allData.data.contents.list[indexPath.section - 1].name;
//        reuseableView.backgroundColor = [UIColor orangeColor];
        return reuseableView;
    }
    
}

#pragma mark - 返回item的大小
- (CGSize)collectionView:(UICollectionView *)collectionView
                  layout:(UICollectionViewLayout *)collectionViewLayout
  sizeForItemAtIndexPath:(NSIndexPath *)indexPath {

    if (indexPath.row == 0) {
        return CGSizeMake(CGRectGetWidth(collectionView.bounds) - 10, CGRectGetHeight(collectionView.bounds) / 2 - 15);
    } else {
        return CGSizeMake((CGRectGetWidth(collectionView.bounds) - 15) / 2, CGRectGetHeight(collectionView.bounds) / 4);
    }
}
#pragma mark - 返回头视图的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return CGSizeMake(self.view.bounds.size.width, 250);
    } else {
        return CGSizeMake(self.view.bounds.size.width, 50);
    }
}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
